﻿namespace LeadMaster.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using LeadMaster.Models;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;
    using System.Web.Security;

    public class CompaniesController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();

        public ActionResult Index()
        {
            string companyCategory= Roles.IsUserInRole("Operations") ? "O" : "C" ;
            return View(db.Companies.Where(m => m.Category==companyCategory).OrderBy(m => m.Company).ThenBy(m => m.Town).ToList());
        }

        public ActionResult PanelIndex()
        {
            string companyCategory = Roles.IsUserInRole("Operations") ? "O" : "C";
            List<Companies> listCompanies = db.Companies.Include(m => m.Contacts).Where(m => m.Category == companyCategory).OrderBy(m => m.Company).ThenBy(m => m.Town).ToList();
            return PartialView("Index", listCompanies); 
        }

        public ActionResult Details(int id = 0)
        {
            ViewModelContacts viewModel = new ViewModelContacts();
            viewModel.Company=db.Companies.Find(id);
            viewModel.Contacts = db.Contacts.Where(m => m.CompanyID == id).OrderBy(m => m.Surname).ThenBy(m => m.Forename);
            if (viewModel.Company == null)
            {
                return HttpNotFound();
            }
            return PartialView(viewModel);
        }

        public ActionResult Create()
        {
            ViewModelCompanies viewModel = new ViewModelCompanies();
            viewModel.Company = new Companies();
            ViewBag.SectorID = new SelectList(db.Sectors.OrderBy(m => m.Sector), "SectorID", "Sector");

            return PartialView(viewModel);
        }

        [HttpPost]
        public ActionResult Create(ViewModelCompanies viewModel)
        {
            if (Roles.IsUserInRole("Operations"))
            {
                viewModel.Company.Category = "O";
            }
            else
            {
                viewModel.Company.Category = "C";
            }
            if (ModelState.IsValid)
            {
                db.Companies.Add(viewModel.Company);
                db.SaveChanges();

                return RedirectToAction("Index","Home");
            }

            return RedirectToAction("Index","Home");
        }

        public ActionResult Edit(int id = 0)
        {
            ViewModelCompanies viewModel = new ViewModelCompanies();
            viewModel.Company = db.Companies.Find(id);
            if (viewModel.Company == null)
            {
                return HttpNotFound();
            }

            return PartialView(viewModel);
        }

        [HttpPost]
        public ActionResult Edit(ViewModelCompanies viewModel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(viewModel.Company).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index","Home");
            }
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Delete(int id = 0)
        {
            Companies companies = db.Companies.Find(id);
            if (companies == null)
            {
                return HttpNotFound();
            }
            return View(companies);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Companies companies = db.Companies.Find(id);
            db.Companies.Remove(companies);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public static string GetColourCode(int companyID)
        {
            DunsmoreContext db = new DunsmoreContext();
            Enquiries enquiry= db.Enquiry.Where(m => m.CompanyID==companyID).OrderByDescending(m => m.EnquiryDate).FirstOrDefault() ;
            string colourCode;
            if (enquiry==null)
            {
                colourCode = "000000";
            }
            else
            {
                colourCode = db.EnquiryTypes.Where(m => m.EnquiryTypeID == enquiry.EnquiryTypeID).Select(m => m.ColourCode).FirstOrDefault();
            }
            
            return "#" + colourCode;
        }
    }
}